package com.bryant.demo05.ui.detail

// ================================ 该文件为MVP模式中的Presenter层， 充当Model 和 View 之间的中介。================================
// ================================ 也提供方法供Activity层调用，并实现业务逻辑=================================

// ----------------------------------------------------------------------------------------------------------------------------

import com.bryant.demo05.base.BasePresenter

class WeatherDetailPresenter(
    val mView: WeatherDetailView,
    val mInteractor: WeatherDetailInteractor
) :
    BasePresenter<WeatherDetailView, WeatherDetailInteractor>(mView, mInteractor),
    WeatherDetailInteractor.Get3DayWeatherCallBack {
    // 存储城市名和经纬度
    private var mCityName = ""
    private var mLatitude = ""
    private var mLongitude = ""

    // 设置城市名
    fun setCityName(cityName: String) {
        mCityName = cityName
    }

    // 设置经纬度
    fun setLocation(latitude: String, longitude: String) {
        mLatitude = latitude
        mLongitude = longitude
    }


//     *  获取3天天气
//     *  参数：经纬度

    //当 WeatherDetailInteractor 执行 get3DayWeather 方法时，会回调 WeatherDetailPresenter 中实现的相应方法
    //这样 Presenter 就能获取到数据并更新 WeatherDetailView
    fun get3DayWeather() {
        mInteractor.get3DayWeather(mLatitude, mLongitude, this)
    }


//     * 获取3天天气成功


    override fun get3DayWeatherSuccess(weather: com.qweather.sdk.response.weather.WeatherDailyResponse) {

     /*    * 使用作用域函数处理天气数据更新
         * 通过run函数简化对weather对象的操作，直接访问其属性和方法
         * 将今日天气数据通知到视图界面*/

        weather.run {
            mView.refreshTodayDataSuccess(weather.daily[0])
        }
    }



//     * 获取3天天气失败

    override fun get3DayWeatherFailure(error: kotlin.String) {
        mView.refreshTodayDataFailed(error)
    }
}


